package com.github.zdg.recyclerview;

import android.util.SparseArray;


/**
 * Created by zoudong on 2017/6/8.
 */

class ViewHolderHelper<V> {

    private SparseArray<V> mViewHolders = new SparseArray<>();

    public int addViewHolder(int key, V holder) {
        mViewHolders.put(key, holder);
        return mViewHolders.indexOfKey(key);
    }

    public V get(int key) {
        return mViewHolders.get(key);
    }

    public int removeViewHolder(V holder) {
        int indexOfValue = mViewHolders.indexOfValue(holder);
        return removeViewHolder(indexOfValue);
    }

    /**
     * @param index addViewHolder retrun
     */
    public int removeViewHolder(int index) {
        if (index>-1&&index<mViewHolders.size()) {
            mViewHolders.removeAt(index);
            return index;
        }
        return -1;
    }

    public boolean isContains(V holder) {
        return indexOfValue(holder) > -1;
    }


    public int keyAt(int index) {
        return mViewHolders.keyAt(index);
    }

    public int indexOfValue(V holder) {
        return mViewHolders.indexOfValue(holder);
    }

    public int size() {
        return mViewHolders.size();
    }

    public boolean isContains(int key) {
        return mViewHolders.get(key) != null;
    }
}
